package core;

import bgg4j.*;

import java.io.File;
import java.io.IOException;
import java.util.*;

import ui.WareHouse;

public class Library {
	
	private Bgg bgg = null;
	private String m_username = null;
	private List<CollectedGame> m_list_collection;
	private List<GameInfo> m_list_gameinfo;
	private List<QueryResult> m_list_result;
	public static String HOMEDIR = ".MyGameWareHouse/";
	
	public Library(String username){
		bgg = new Bgg();
		bgg.setDir(Library.HOMEDIR);
		m_username = username;
	}
	
	public void search(String key, boolean isExact) throws BggException{
		m_list_result = bgg.getQueryResult(key, isExact);
	}
	
	public int getResultSize(){
		return m_list_result.size();
	}
	
	public Object[] getResultRawData(int index){
		QueryResult game = m_list_result.get(index);
		ResultRawData rawdata = new ResultRawData();
		rawdata.setGameID(game.getGameID());
		rawdata.setTitle(game.getName());
		rawdata.setYear(game.getYear());
		return rawdata.getRawData();
	}
	
	public String getBGGURL(){
		return bgg.getBaseURL();
	}
	
	public void setUserID(String username){
		m_username = username;
	}
	
	public int size(){
		return m_list_gameinfo.size();
	}
	
	private double getRating(int gameID){
		double ret=0;
		for (CollectedGame game : m_list_collection){
			if (game.getGameID() == gameID){
				ret = game.getRating();
				break;
			}
		}
		return ret;
	}
	
	public Object[] getTableRawData(int index){
		GameInfo game = m_list_gameinfo.get(index);
		TableRawData rawdata = new TableRawData();
		rawdata.setGameID(game.getGameID());
		rawdata.setTitle(game.getName());
		rawdata.setDesigner(game.getDesigner());
		rawdata.setMax(game.getMaxPlayer());
		rawdata.setMin(game.getMinPlayer());
		rawdata.setRank(game.getRank());
		rawdata.setYear(game.getYear());
		rawdata.setTime(game.getPlayingTime());
		rawdata.setRating(this.getRating(game.getGameID()));
		rawdata.setBayesAverage(game.getBayesAverage());
		return rawdata.getRawData();
	}
	
	public void Update() throws BggException, IOException{
		System.out.println("Update " + m_username);
		bgg.getSnapShotCollection(m_username);
		m_list_collection = bgg.loadSnapshotCollection(m_username);
		StringBuffer strGameID = new StringBuffer();
        for (CollectedGame game : m_list_collection) {
            strGameID.append(game.getGameID() + ",");
        }
        bgg.getSnapShotGameInfo(m_username, strGameID.toString());
	}
	
	public void updateImage(WareHouse owner){
		int count = 0;
		for (CollectedGame game : m_list_collection){
			try {
				count++;
				owner.setStatus("download thumbnail ... [" + game.getName() + "]");
				String file = Library.HOMEDIR + "imagecache/" + String.valueOf(game.getGameID()) + ".jpg";
				if ( (!(new File(file).exists()))){
					bgg.getFile(game.getThumbnail(), file);
				}
			} catch (BggException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
				owner.setStatus("Error:" + e.getStatusCode() + "[" + e.getMessage() + "]");
			} catch (IOException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
		}
	}
	
	public String[] getImageArray(){
		String[] ret = new String[m_list_collection.size()];
		int count = 0;
		for (CollectedGame game : m_list_collection){
			ret[count] = new String(Library.HOMEDIR + "imagecache/" + String.valueOf(game.getGameID()) + ".jpg");
			count++;
		}
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public void load(){
		try {
			m_list_collection = bgg.loadSnapshotCollection(m_username);
			//System.out.println("collection:" + m_list_collection.size());
			m_list_gameinfo = bgg.loadSnapshotGameInfo(m_username);
			RankComparator rankComparator = new RankComparator();
			Collections.sort(m_list_gameinfo, rankComparator);
			//System.out.println("info:" + m_list_gameinfo.size());
		} catch (BggException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		
	}
}

class RankComparator implements Comparator {

	public int compare(Object o1, Object o2) {
		GameInfo game1 = (GameInfo)o1;
		GameInfo game2 = (GameInfo)o2;
		int ret;
		if (game1.getRank() < 0){
			ret = 1;
		}else if (game2.getRank() < 0){
			ret = -1;
		}else{
			ret = game1.getRank() - game2.getRank();
		}
		return ret;
	}
	
	public boolean equals(Object o1, Object o2) {
		GameInfo game1 = (GameInfo)o1;
		GameInfo game2 = (GameInfo)o2;
		return (game1.getRank() == game2.getRank());
	}
}
